import librosa
import librosa.display
import matplotlib.pyplot as plt


def getMFCC(filepath, name, makirpath):
    y, sr = librosa.load(path=filepath, sr=None)  # sr采样率，默认22050 返回值y : 音频的信号值，类型是ndarray sr : 采样率
    mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=24)  # 要返回的mfcc数量
    mfccspec = librosa.power_to_db(mfccs)
    plt.figure()
    librosa.display.specshow(mfccspec, sr=sr)
    plt.show()



def getMel(filepath, name, makirpath):
    y, sr = librosa.load(path=filepath, sr=None)
    melspec = librosa.feature.melspectrogram(y, sr, n_fft=1024, hop_length=512, n_mels=128)
    logmelspec = librosa.power_to_db(melspec)
    plt.figure()
    fig = plt.figure(figsize=(12.8, 12.8), dpi=20, frameon=False)  # 像素点20，不显示边框
    heatmap = plt.pcolor(logmelspec)  # 根据spec的值，画色彩图
    plt.xticks([])
    plt.yticks([])
    plt.tight_layout()
    path = makirpath+'\\'+name+'.png'

    plt.savefig(path)
    plt.close()
